class Node:
    def __init__(self, val):
        self.val = val
        self.prev = None
        self.next = None


class BrowserHistory(object):

    def __init__(self, homepage):
        """
        :type homepage: str
        """
        self.head = Node(homepage)
        self.last = Node('#')
        self.head.next = self.last
        self.last.prev = self.head
        self.now = self.head

    def visit(self, url):
        """
        :type url: str
        :rtype: None
        """
        new_node = Node(url)
        self.now.next = new_node
        new_node.next = self.last
        self.last.prev = new_node
        new_node.prev = self.now
        self.now = new_node

    def back(self, steps):
        """
        :type steps: int
        :rtype: str
        """
        while self.now != self.head and steps > 0:
            self.now = self.now.prev
            steps -= 1
        return self.now.val

    def forward(self, steps):
        """
        :type steps: int
        :rtype: str
        """
        while self.now != self.last and steps > 0:
            self.now = self.now.next
            steps -= 1
        if self.now == self.last:
            self.now = self.last.prev
        return self.now.val

obj = BrowserHistory("leetcode.com")
obj.visit('1')
obj.visit('2')
obj.back(1)
obj.back(1)
obj.forward(1)
obj.visit('3')
obj.forward(2)
obj.back(2)
obj.back(7)


